package com.china08.yxyapi.api.manage.tec.office.moments;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.moments.YxyMoments;
import com.china08.yxyapi.entity.mg.moments.YxyMomentsComment;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsCommentRepository;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

/**
 * 朋友圈--老师--发表评论/回复
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class MomentsPublishCommentController extends YxyApiBaseController {

    @Autowired
    private YxyMomentsRepository yxyMomentsRepository;

    @Autowired
    private YxyMomentsCommentRepository yxyMomentsCommentRepository;

    @Autowired
    private YxUserPointsUtilV3 yxUserPointsUtilV3;

    @RequestMapping(value = "/api_yw/manage/tec/office/moments/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request,
            HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {
        YxyProfile yxyProfile = getProfile(request, response);

        YxyMoments moments = yxyMomentsRepository.findById(reqModel.momentsId);
        Assert.notNull(moments, "班级圈不存在");

        // 当前用户
        String currUserId = yxyProfile.getId();
        String currUserNick = yxyProfile.getUserNick();
        String currAvatar = yxyProfile.getUserAvator();
        //
        String momentsId = reqModel.momentsId;
        String replyerNick = reqModel.replyerNick;
        String replyerId = reqModel.replyerId;
        String comment = reqModel.comment;

        YxyMomentsComment yxyMomentsComment = new YxyMomentsComment();
        yxyMomentsComment.setUserId(currUserId);
        yxyMomentsComment.setUserNick(currUserNick);
        yxyMomentsComment.setAvatar(currAvatar);
        //
        yxyMomentsComment.setMomentsId(momentsId);
        //
        yxyMomentsComment.setReplyerId(replyerId);
        yxyMomentsComment.setReplyerNick(replyerNick);
        yxyMomentsComment.setComment(comment);
        yxyMomentsCommentRepository.save(yxyMomentsComment);
        yxUserPointsUtilV3.commentClassMessage(currUserId);

        // 评论数量+1
        Integer commentCount = (moments.getCommentCount() == null) ? 1
                : moments.getCommentCount() + 1;// 数量不能为空
        moments.setCommentCount(commentCount);
        yxyMomentsRepository.save(moments);
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        // 班级圈 ID
        public String momentsId;
        // 被回复人 Id（如果评论班级圈不传）
        public String replyerId;
        // 被回复人 昵称（如果评论班级圈不传）
        public String replyerNick;
        // 内容
        public String comment;
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
